<?php

namespace app\api\controller;

use app\admin\model\Advert;
use app\admin\model\Article;
use app\admin\model\ArticleCategory;
use app\admin\model\Attr;
use app\admin\model\Complain;
use app\admin\model\Cooperation;
use app\admin\model\Hotword;
use app\admin\model\Industry;
use app\admin\model\Nav;
use app\admin\model\Notice;
use app\admin\model\Rule;
use app\api\model\group\Group as GroupModel;
use app\api\model\group\GroupEvaluate;
use app\api\model\house\House as HouseModel;
use app\api\model\house\HouseEvaluate as HouseEvaluateModel;
use app\api\model\project\ProjectEvaluate as ProjectEvaluateModel;
use app\api\model\project\ProjectStaff as ProjectStaffModel;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $project_zan_num = ProjectStaffModel::sum('zan_num');
        $project_collect_num = ProjectStaffModel::sum('collect_num');
        $project_repost_num = ProjectStaffModel::sum('repost_num');
        $project_comment_num = ProjectEvaluateModel::count();

        $house_zan_num = HouseModel::sum('zan_num');
        $house_collect_num = HouseModel::sum('collect_num');
        $house_repost_num = HouseModel::sum('repost_num');
        $house_comment_num = HouseEvaluateModel::count();

        $group_zan_num = GroupModel::sum('zan_num');
        $group_collect_num = GroupModel::sum('collect_num');
        $group_repost_num = GroupModel::sum('repost_num');
        $group_comment_num = GroupEvaluate::count();

        $data = [
            'zan_num'=>$project_zan_num+$house_zan_num+$group_zan_num,
            'collect_num'=>$project_collect_num+$house_collect_num+$group_collect_num,
            'repost_num'=>$project_repost_num+$house_repost_num+$group_repost_num,
            'comment_num'=>$project_comment_num+$house_comment_num+$group_comment_num,
        ];
        $this->success('请求成功',$data);
    }

    //轮播
    public function banner()
    {
        $module = input('module','open');
        $list = Advert::where(['module'=>$module,'status'=>'normal'])->order('weigh desc')->select();
        $this->success('请求成功',$list);
    }

    //导航
    public function nav()
    {
        $module = input('module','open');
        $list = Nav::where(['module'=>$module,'status'=>'normal'])->order('weigh desc')->select();
        $this->success('请求成功',$list);
    }

    //文章分类
    public function articleCategory()
    {
        $list = ArticleCategory::order('weigh desc')->field('category_id,name')->select();
        $this->success('请求成功',$list);
    }

    //文章列表
    public function article()
    {
        $category_id = input('category_id',0);

        $where = [];
        if ($category_id){
            $where['category_id'] = $category_id;
        }
        $list = Article::where($where)->field('article_id,category_id,title,image,top_image,is_top,is_recommend,createtime')
            ->order('is_top desc is_recommend desc weigh desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('请求成功',$list);
    }

    //文章详情
    public function articleInfo()
    {
        $id = input('article_id',0);

        $info = Article::get($id);
        //$info->content = '<font><span style="font-size: 36px;">'.$info->content.'</span></font>';
        $this->success('请求成功',$info);
    }

    //规则列表
    public function rule()
    {
        $list = Rule::order('weigh desc')->field('id,name')->select();
        $this->success('请求成功',$list);
    }

    //规则详情
    public function ruleInfo()
    {
        $id = input('id',0);

        $info = Rule::get($id);

        $this->success('请求成功',$info);
    }

    //合作企业
    public function cooperation()
    {
        $list = Cooperation::order('weigh desc')->field('id,name,logo')->select();

        $this->success('请求成功',$list);
    }

    //热门搜索词
    public function hotword()
    {
        $type = input('type',0);
        $list = Hotword::where(['type'=>$type])->order('weigh desc')->column('name');
        $this->success('请求成功',$list);
    }

    //投诉原因
    public function complain()
    {
        $type = input('type',0);
        $list = Complain::where(['type'=>$type])->order('weigh desc')->column('name');
        $this->success('请求成功',$list);
    }

    //版本号
    public function version()
    {
        $value = \app\common\model\Config::get(['name'=>'version'])->value;

        $this->success('请求成功',$value);
    }

    //协议配置
    public function protocol()
    {
        $name = input('name','aboutus');

        $value = \app\common\model\Config::get(['name'=>$name])->value;

        $this->success('请求成功',$value);
    }

    //公告列表
    public function notice()
    {
        $list = Notice::order('weigh desc')->field('id,name')->select();

        $this->success('请求成功',$list);
    }

    //广告
    public function adv()
    {
        $data = [
            'service_adv_one'=>\app\common\model\Config::get(['name'=>'service_adv_one'])->value,
            'service_adv_two'=>\app\common\model\Config::get(['name'=>'service_adv_two'])->value,
            'group_adv_one'=>\app\common\model\Config::get(['name'=>'group_adv_one'])->value,
            'group_adv_two'=>\app\common\model\Config::get(['name'=>'group_adv_two'])->value,
            'house_adv_one'=>\app\common\model\Config::get(['name'=>'house_adv_one'])->value,
        ];
        $this->success('请求成功',$data);
    }


    //开屏广告
    public function openadvert()
    {
        $data = [
            'open_advert_img'=>\app\common\model\Config::get(['name'=>'open_advert_img'])->value,
            'play_type'=>\app\common\model\Config::get(['name'=>'play_type'])->value,
            'play_time'=>\app\common\model\Config::get(['name'=>'play_time'])->value,
        ];
        $this->success('请求成功',$data);
    }

    //举报配置
    public function report()
    {
        $data = [
            'report_mobile'=>\app\common\model\Config::get(['name'=>'report_mobile'])->value,
            'report_email'=>\app\common\model\Config::get(['name'=>'report_email'])->value,
        ];
        $this->success('请求成功',$data);
    }

    //单位属性
    public function attr()
    {
        $list = Attr::order('weigh desc')->column('name');
        $this->success('请求成功',$list);
    }

    //所属行业
    public function industry()
    {
        $list = Industry::order('weigh desc')->column('name');
        $this->success('请求成功',$list);
    }

    //组团团长介绍
    public function group()
    {
        $group_team_intro = \app\common\model\Config::get(['name'=>'group_team_intro'])->value;
        $this->success('请求成功',$group_team_intro);
    }
}
